<?php

namespace App\Validators\Personnel;

use App\Validators\PassesValidator;
use Illuminate\Support\Facades\Input;
use \Prettus\Validator\Contracts\ValidatorInterface;
use \Prettus\Validator\LaravelValidator;

class StationClassValidator extends LaravelValidator{

    use PassesValidator;

    protected $rulesMessages=[
        'name.required' => '分类名称是必填的',
        'name.unique' => '该分类名称已经存在',
        'id.not_exists' => '该分类还有岗位维护不能删除',];

    protected $rules = [
        ValidatorInterface::RULE_CREATE => [
            'name'=>'required|unique:station_class'
        ],
        ValidatorInterface::RULE_UPDATE => [
            'name'=>'required|unique:station_class,name,{id}'
        ],
        "DELETE" => [
            'id' =>'not_exists:station,station_class_id',
        ]
   ];

    public function callBackGetRules(array $rules){
        $result=[];
        $value_change = array('{id}'=>Input::get('id'));
        foreach ($rules as $key=>$value){
            if(is_string($value) && str_contains($value,'{id}')){
                $result[$key]=strtr($value,$value_change);
            }else{
                $result[$key]=$value;
            }
        }
        return $result;
    }
}
